package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.application.CyclingApplication;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.ActivityStackManager;
import com.boyi.cycling_master.common.PostParams;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.https.utils.UserInfoRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date 2017/7/26/026 15:50
 * @Description 设置中修改密码
 */

public class PwdChangeActivity extends BaseActivity {
    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_update_password_old)
    MaterialEditText edtUpdatePasswordOld;
    @BindView(R.id.edt_update_password_new)
    MaterialEditText edtUpdatePasswordNew;
    @BindView(R.id.edt_update_password_confirm)
    MaterialEditText edtUpdatePasswordConfirm;
    @BindView(R.id.tv_update_pwd_commit)
    TextView tvUpdatePwdCommit;
    @BindView(R.id.tv_update_pwd_forget_pwd)
    TextView tvUpdatePwdForgetPwd;

    @Override
    public int getContentViewId() {
        return R.layout.activity_change_passwrod;
    }

    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {

    }


    @OnClick({R.id.btn_nav_icon, R.id.tv_update_pwd_commit, R.id.tv_update_pwd_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:// 返回
                finish();
                break;
            case R.id.tv_update_pwd_commit:// 修改提交
                String oldPwd = edtUpdatePasswordOld.getText().toString();
                String newPwd = edtUpdatePasswordNew.getText().toString();
                String confirmPwd = edtUpdatePasswordConfirm.getText().toString();
                if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(confirmPwd)) {
                    SnackUtil.SnackShort(edtUpdatePasswordConfirm, "请您输入相关信息");
                } else {
                    if (TextUtils.equals(newPwd, confirmPwd)) {
                        Map<String, String> map = new HashMap<>();
                        map.put("oldPassword", oldPwd);
                        map.put("newPassword", newPwd);
                        /* 提交新密码到服务器 */
                        UserInfoRetrofit.getInstance().changePwd(new Callback<ResponseBean>() {
                            @Override
                            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                                if (response.body() == null)
                                    return;
                                SnackUtil.SnackShort(edtUpdatePasswordConfirm, response.body().getResultMessage());
                                if (response.body().getResultCode().equals("200")) {// 密码修改成功  并且重新登陆
                                    ActivityStackManager.getInstance().killAllActivity();// 退出当前所有act
                                    CyclingApplication.getInstance().getUserInfo().delete();// 清空用户信息
                                    Intent intent = new Intent(PwdChangeActivity.this, PhoneLoginActivity.class);// 重新进行登录
                                    startActivity(intent);
                                }
                            }

                            @Override
                            public void onFailure(Call<ResponseBean> call, Throwable t) {

                            }
                        }, PostParams.makeRequestBody(map));
                    } else {
                        SnackUtil.SnackShort(edtUpdatePasswordConfirm, "新密码确认有误");
                    }
                }
                break;
            case R.id.tv_update_pwd_forget_pwd:// 忘记密码
                break;
        }
    }
}
